package com.yanxu;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2023/11/18-2023
 * @Description: 生产者生产消息
 */
public class ProducerSample01 {
    public static void main(String[] args) {
        //定义生产者对象
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("yx-producer");
        //设置 nameServer 的地址。（rocketmq 注册中心的地址）
        defaultMQProducer.setNamesrvAddr("192.168.1.30:9876");

        //开启。建立长连接
        try {
            defaultMQProducer.start();
            for (int i = 0; i < 100; i++) {
                String messageTex="{\n" +
                        "  \"id\": 1001,\n" +
                        "  \"type\": \"site\",\n" +
                        "  \"name\": \"Runoob\",\n" +
                        "  \"description\": \"https://www.runoob.com\",\n" +
                        "  \"price\": 2.55\n" +
                        "}\n";

                //定义消息，进行消息的发送
                //Message message = new Message("test-01", "text-001", messageTex.getBytes(StandardCharsets.UTF_8));
                Message message = new Message("test-01",  messageTex.getBytes(StandardCharsets.UTF_8));
                SendResult result = defaultMQProducer.send(message);

                //返回结果输出
                System.out.println(result.getMsgId());
                System.out.println(result.getSendStatus());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭操作
            defaultMQProducer.shutdown();
        }
    }
}